<?php

namespace App\Admin\Controllers\Login;

use Illuminate\Http\Request;

use App\Http\Controllers\Controller;
use App\Admin\Models\AdminUser;
use App\Common\Services\Admin\Common;
use Illuminate\Support\Facades\Redis;

class LoginController extends Controller
{

    /**
     * 超管登陆
     */
    public function dologin(Request $request)
    {
        try {
            $this->validate($request, [
                'username' => 'required',
                'password' => 'required',
            ]);
        } catch (\Exception $exception) {
            return response()->json(['code' => '001', 'msg' => '帐号或者密码不能为空']);
        }
        if (strlen($request->password) != 32) {
            return response()->json(['code' => '001', 'msg' => '密码长度不规范']);
        }
        $info = AdminUser::where('username', $request->username)->where('password', MD5($request->password))->first();
        if ($info) {
            if ($info->status != 1) {

                return response()->json(['code' => '001', 'msg' => '账号已被禁用']);
            }
            $admintoken = md5(time() . mt_rand(1000, 9999) . '__' . $info->id);
            Redis::set($admintoken, json_encode($info));
            Common::storetokenkey($admintoken, $info->id);

            AdminUser::where(['id' => $info->id])->update(['lastlogin_ip' => $request->ip(), 'lastlogin_at' => time()]);
            $info = $info->toArray();
            unset($info['password']);
            return response()->json([
                'code' => '000', 'msg' => '登录成功', 'data' => ['admintoken' => $admintoken, 'info' => $info]
            ]);
        } else {
            return response()->json(['code' => '001', 'msg' => '帐号或者密码错误']);
        }
    }

    /**
     * 超管退出登录
     */
    public function dologinout(Request $request)
    {
        $admintoken = $request->header('admintoken');
        $memberinfo = Redis::get($admintoken);
        if (!$admintoken || !$memberinfo) {
            return response()->json(['code' => '001', 'msg' => '非法访问']);
        }
        $memberinfo = json_decode($memberinfo, true);
        $info = Adminuser::where(['id' => $memberinfo['id']])->first();
        if (!$info) {
            return response()->json(['code' => '001', 'msg' => '无效参数']);
        }

        Redis::delete($admintoken);
        return response()->json(['code' => '000', 'msg' => '操作成功']);
    }

    /**
     * 超管更改密码
     */
    public function changepassword(Request $request)
    {
        $admintoken = $request->header('admintoken');
        $memberinfo = Redis::get($admintoken);
        if (!$admintoken || !$memberinfo) {
            return response()->json(['code' => '001', 'msg' => '非法访问']);
        }


        $memberinfo = json_decode($memberinfo, true);
        $info = Adminuser::where(['id' => $memberinfo['id']])->first();
        if (!$info) {
            return response()->json(['code' => '001', 'msg' => '无效参数']);
        }
        $oldpassword = $request->oldpassword;
        $password = $request->password;
        $repassword = $request->repassword;
        if ((strlen($oldpassword) < 5 || strlen($oldpassword) > 20) || (strlen($password) < 5 || strlen($password) > 20) || (strlen($repassword) < 5 || strlen($repassword) > 20)) {
            return response()->json(['code' => '001', 'msg' => '请保持密码长度为5~20位']);
        }

        if ($oldpassword == '' || $password == '' || $repassword == '' || $password != $repassword) {
            return response()->json(['code' => '001', 'msg' => '请确保输入框完整']);
        }


        if (MD5(MD5('zhuxiaobao_' . $oldpassword)) != $info->password) {
            return response()->json(['code' => '001', 'msg' => '原密码不正确']);
        }

        Adminuser::where(['id' => $memberinfo['id']])->update(['password' => MD5(MD5('zhuxiaobao_' . $password))]);

        //清除缓存
        Common::cleartokenkey($memberinfo['id']);
        return response()->json(['code' => '000', 'msg' => '操作成功']);
    }
}
